package com.linshengjian.power.service.imp;

import com.linshengjian.core.BaseDao;
import com.linshengjian.core.BaseService;
import com.linshengjian.core.BusinessException;
import com.linshengjian.power.dao.PermissionDao;
import com.linshengjian.power.dao.ResourcesDao;
import com.linshengjian.power.entity.PermissionEntity;
import com.linshengjian.power.entity.ResourcesEntity;
import com.linshengjian.power.model.Permission;
import com.linshengjian.power.model.Resources;
import com.linshengjian.power.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Set;

@Service
public class PermissionService extends BaseService<PermissionEntity, Permission, Integer> implements IPermissionService {

    private final static String permissionNotFind = "权限对象不存在";
    private final static String resourcesNotFind = "资源对象不存在";

    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private ResourcesService resourcesService;

    @Override
    protected BaseDao<PermissionEntity, Integer> dao() {
        return permissionDao;
    }

    @Override
    protected PermissionEntity model2Entity(Permission model) {
        PermissionEntity entity = new PermissionEntity();
        entity.setId(model.getId());
        entity.setCode(model.getCode())
                .setName(model.getName())
                .setDescription(model.getDescription());
        return entity;
    }

    @Override
    protected Permission entity2model(PermissionEntity entity) {
        Permission model = new Permission();
        model.setId(entity.getId());
        model.setCode(entity.getCode())
                .setName(entity.getName())
                .setDescription(entity.getDescription());
        return model;
    }

    @Override
    protected List<Permission> entity2Model(Collection<PermissionEntity> list) {
        return super.entity2Model(list);
    }

    @Override
    public void addResources(Integer pid, Integer rid) throws BusinessException {
        PermissionEntity permission = dao().findOne(pid);
        if (permission == null)
            throw new BusinessException(permissionNotFind);
        ResourcesEntity resources = resourcesService.dao().findOne(rid);
        if (resources == null)
            throw new BusinessException(resourcesNotFind);

        permission.getResources().add(resources);

        dao().save(permission);
    }

    @Override
    public void delResources(Integer pid, Integer rid) throws BusinessException {
        PermissionEntity permission = dao().findOne(pid);
        if (permission == null)
            throw new BusinessException(permissionNotFind);
        for (ResourcesEntity res: permission.getResources()) {
            if (res.getId().equals(rid)) {
                permission.getResources().remove(res);
                break;
            }
        }
        dao().save(permission);
    }

    @Override
    public List<Resources> resources(Integer id) throws BusinessException {
        PermissionEntity permission = dao().findOne(id);
        if (permission == null)
            throw new BusinessException(permissionNotFind);

        return resourcesService.entity2Model(permission.getResources());
    }
}
